import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);

// 解决路由重读挂载报错问题
//#region
// 获取原型对象push函数
const originalPush = VueRouter.prototype.push

// 获取原型对象replace函数
const originalReplace = VueRouter.prototype.replace

// 修改原型对象中的push函数
VueRouter.prototype.push = function push(location){
return originalPush.call(this , location).catch(err=>err)
}

// 修改原型对象中的replace函数
VueRouter.prototype.replace = function replace(location){
return originalReplace.call(this , location).catch(err=>err)
}
//#endregion

const routes = [
  {
    path:"/",
    redirect: "/shop"
  },
  // 前台
  // 前台登录
  {
    path:"/login",
    name:"login",
    component: ()=> import("../views/shopSystem/login/login.vue")
  },

  {
    path: "/shop",
    name: "shop",
    component: ()=> import("../views/shopSystem/index.vue"),
    children:[
      {
        path:"/shop",
        redirect:"index"
      },
      {
        // 首页
        path:"index",
        name:"index",
        component: ()=> import("../views/shopSystem/home/home.vue"),

      },
      {
        // 分类
        path:"sort",
        name:"sort",
        component: ()=> import("../views/shopSystem/sort/sort.vue"),
      },
      {
        // 购物车
        path:"cart",
        name:"cart",
        component: ()=> import("../views/shopSystem/shopCart/shopCart.vue"),
      },
      {
        // 我的
        path:"my",
        name:"my",
        component: ()=> import("../views/shopSystem/my/my.vue"),
      }
    ]
  },
  // 后台
  // 后台登录
  {
    path:"/admin-login",
    name:"admin-login",
    component:()=> import ("../views/BMS/login/login.vue")
    
  },
  {
    path:"/bms",
    name:"bms",
    component:()=> import ("../views/BMS/index.vue"),
    children:[
      {
        path:"/bms",
        redirect:"home"

      },
      // 首页
      {
        path:"home",
        name:"home",
        component:()=> import ("../views/BMS/home/home.vue")
      },
      /* 商品管理 */
      // 商品管理
      {
        path:"product-manage",
        name:"product-manage",
        component:()=> import ("../views/BMS/productManage/productManagement.vue")
      },
      // 商品发布
      {
        path:"product-publish",
        name:"product-publish",
        component:()=> import ("../views/BMS/productManage/productPublish.vue")
      },
      // 商品评论
      {
        path:"product-comments",
        name:"product-comments",
        component:()=> import ("../views/BMS/productManage/productComments.vue")
      },
      /* 订单管理 */
      // 订单管理
      {
        path:"order-manage",
        name:"order-manage",
        component:()=> import ("../views/BMS/orderManage/orderManagement.vue")
      },
      // 退款管理
      {
        path:"order-refunds",
        name:"order-refunds",
        component:()=> import ("../views/BMS/orderManage/orderManageRefunds.vue")
      },

       /* 账号管理 */
      //  用户管理
      {
        path:"user-manage",
        name:"user-manage",
        component:()=> import ("../views/BMS/accountManage/userManagement.vue")
      },

      /* 设置 */
      // 个人资料
      {
        path:"person-info",
        name:"person-info",
        component:()=> import ("../views/BMS/bmsSetting/personInfo.vue")
      },
      // 安全设置
      {
        path:"safe-setting",
        name:"safe-setting",
        component:()=> import ("../views/BMS/bmsSetting/safeSetting.vue")
      }

      
    ]
  }

];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  // 根据路由判断加载前台或后台 JavaScript 文件
  const jsFileName = to.path.startsWith('/bms') ? 'admin.js' : 'front.js';

  // 动态加载 JavaScript 文件
  import(`@/assets/${jsFileName}`).then(() => {
    next();
  });
});

export default router;
